﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data;
using System.Data.Objects;
using System.Data.Common;
using System.Data.Entity;

using MINERP.DAL;
using MINERP.MODEL.Statistics;
using MINERP.DAL.Statistics;
using MINERP.COM;

namespace MINERP.BLL.Statistics
{
    public  class InvoiceBLL
    {
        InvoiceDAL _dal = new InvoiceDAL();
        /// <summary>
        /// 获取业务员（总店名）列表
        /// </summary>
        /// <returns></returns>
        public List<Salesman_Info> GetSalemanList()
        {
            List<Salesman_Info> list = new List<Salesman_Info>();
            using (newSampleYPDBEntities db = new newSampleYPDBEntities())
            {
                List<SalesmanBalance> saleManlist = db.SalesmanBalance.ToList();
                foreach (var item in saleManlist)
                {
                    Salesman_Info s = new Salesman_Info()
                    {
                        Balance = item.Balance,
                        Remark = item.Remark,
                        SalesmanId = item.SalesmanId,
                        SalesmanName = item.SalesmanName,
                        Tel1 = item.Tel1
                    };
                    list.Add(s);
                    
                }
            }
            return list;
        }

        /// <summary>
        /// 获取条件上的账单列表
        /// </summary>
        /// <param name="salemanName"></param>
        /// <param name="start"></param>
        /// <param name="end"></param>
        /// <returns></returns>
        public List<Bill_Info> GetBillList(string salemanName, DateTime start, DateTime end)
        {
            List<Bill_Info> billInfoList = new List<Bill_Info>();
            using(newSampleYPDBEntities db = new newSampleYPDBEntities())
            {
                List<Shop> shopList = db.Shop.Where(s => s.Salesman == salemanName).ToList();
                foreach (var item in shopList)
                {
                    if (!item.SHOP_Bill.IsLoaded)
                    {
                        item.SHOP_Bill.Load();
                    }
                    foreach (var shopbill in item.SHOP_Bill.Where(sb=>sb.CreateTime<=end&&sb.CreateTime>=start).ToList())
                    {
                        Bill_Info billInfo = new Bill_Info()
                        {   
                            BillID = shopbill.BillId,
                            BillOrderCode = shopbill.BillOrderCode,
                            CreateTime = shopbill.CreateTime,
                            IsChecked = false,
                            ShopID = shopbill.Shop.ShopId,
                            ShopName = shopbill.Shop.shopName

                        };
                        billInfoList.Add(billInfo);
                    }
                }
            }
            return billInfoList.OrderBy(o=>o.BillID).ToList();
        }


        /// <summary>
        /// 获取完成品发送通知数据
        /// </summary>
        /// <param name="billIds"></param>
        /// <returns></returns>
        public DataSet GetSendGoodsNotice(List<int> billIds)
        {
            string stringBills = "";
            foreach (var item in billIds)
            {
                stringBills += item;
                stringBills += ",";
            }
            stringBills = stringBills.TrimEnd(',');
            return _dal.GetSendGoodsNotice(stringBills);
        }


        /// <summary>
        /// 获取PackingList
        /// </summary>
        /// <param name="billIds"></param>
        /// <returns></returns>
        public DataSet GetPackingList(List<int> billIds)
        {
            string stringBills = "";
            foreach (var item in billIds)
            {
                stringBills += item;
                stringBills += ",";
            }
            stringBills = stringBills.TrimEnd(',');
            return _dal.GetPackingList(stringBills);
        }
        
        /// <summary>
        /// 获取GetInvoice
        /// </summary>
        /// <param name="billIds"></param>
        /// <returns></returns>
        public DataSet GetInvoice(List<int> billIds)
        {
            string stringBills = "";
            foreach (var item in billIds)
            {
                stringBills += item;
                stringBills += ",";
            }
            stringBills = stringBills.TrimEnd(',');
            return _dal.GetInvoice(stringBills);
        }

        /// <summary>
        /// 请求书
        /// </summary>
        /// <param name="billIds"></param>
        /// <returns></returns>
        public DataSet GetRequestPaper(List<int> billIds)
        {
            string stringBills = "";
            foreach (var item in billIds)
            {
                stringBills += item;
                stringBills += ",";
            }
            stringBills = stringBills.TrimEnd(',');
            return _dal.GetRequestPaper(stringBills);
        }
    }

    
}
